
//GPS related
var gpsAccuracy;
var gpsHeading;
var gpsSpeed;
var gpsAltitude;
var gpsAltitudeAccuracy;
var gpsLongitude;
var gpsLatitude;
var gpsLastLongitude;
var gpsLastLatitude;
var gpsDeltaLongitude;
var gpsDeltaLatitude;
var orientationAlpha; // Orientation to the north, depends on deviceOrientation...
var orientationBeta;
var orientationGamma;
var deviceOrientation = 0; //Portrait, Landscape, etc...

var gpsThreadWatch;
var gpsThreadMonitorDelta;

initGPS();
function initGPS() {
    if (navigator.geolocation !== undefined) {
        gpsThreadWatch = navigator.geolocation.watchPosition(updateLocation);
        gpsThreadMonitorDelta = setInterval(updateDeltaPosition, 1000);
        window.addEventListener("orientationchange", updateOrientation, false);
        window.addEventListener("deviceorientation", updateDeviceOrientation, false);
    } else {
        updateFooter("Something's wrong");
        alert("Geolocation is not supported by your browser...");

    }
}

function clearGPS() {
    window.navigator.geolocation.clearWatch(gpsThreadWatch);
    clearInterval(gpsThreadMonitorDelta);
    window.removeEventListener("orientationchange");
    window.removeEventListener("deviceorientation");
}

// Updates raw data from the GPS device;
function updateLocation(position) {
    gpsLongitude = position.coords.longitude;
    gpsLatitude = position.coords.latitude;
    gpsAccuracy = position.coords.accuracy;
    gpsSpeed = position.coords.speed;
    gpsAltitude = position.coords.altitude;
    gpsAltitudeAccuracy = position.coords.altitudeAccuracy;
    gpsHeading = position.coords.heading;
    console.log("Updating GPS data");
    if (lastCrombs === undefined){
        getCrombs();
    }

}

// Provides info relative to the last position
function updateDeltaPosition() {
    gpsLastLongitude = gpsLongitude;
    gpsLastLatitude = gpsLatitude;
    gpsDeltaLongitude = gpsLongitude - gpsLastLongitude;
    gpsDeltaLatitude = gpsLatitude - gpsLastLatitude;
    //console.log("Updating GPS Delta data");
}

// Provides an accuracy level in %
function getAccuracyLevel() {
    if (gpsAccuracy > 100) {
        return 0;
    }
    else {
        var level = Math.round(100 - gpsAccuracy);
        return level;
    }
}

// Provides info about the current position being stable
function isPositonLocked() {
    return (gpsDeltaLongitude + gpsDeltaLatitude) === 0;
}

function updateOrientation() {
    deviceOrientation = orientation;
}

function updateDeviceOrientation(e) {
    if (e.webkitCompassHeading){
        orientationAlpha = (e.webkitCompassHeading+window.orientation)%360;
    } else {
        orientationAlpha = (e.alpha+window.orientation)%360;
    }
    orientationBeta = e.beta;
    orientationGamma = e.gamma;
}

function isGPSEnabled() {
    return gpsLongitude !== undefined;
}
//GPS Related end

// For Exporting Values To Widgets
function getGPSData() {
    return gpsHeading;
}

